<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/04/11 20:58:15 $)
// $RCSfile: admin_status.php,v $ : $Revision: 1.3 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

if (!$staffauth->isAdmin()) {
	trigger_error("You do not have permission to access $PHP_SELF", E_USER_ERROR);
}

require_once ("./modules/teamwork/functions_misc.php");
require_once ("./includes/functions_html.php");

$template->loadLanguageSection("admin_teamwork");

/**
* ###############################################
* FUNCTION DECLARATIONS
* ###############################################
*/

/**
* Callback for Calendar Status
*/
function _calendarStatusFields($arg)
{
	global $_SWIFT;
	$record = &$arg[0];
	$fields = &$arg[1];

	$record["title"] = '<a href="index.php?_m=teamwork&_a=editstatus&calendarstatusid='. $record["calendarstatusid"] .'"><img src="'. $_SWIFT["themepath"] .'icon_teamworkstatus.gif" border="0" align="absmiddle" /> <font color="'. $record["statuscolorcode"] .'">'.htmlspecialchars($record["title"]).'</font></a>';

	if ($record["statustype"] == STATUS_TASK)
	{
		$record["statustype"] = '<img src="'. $_SWIFT["themepath"] .'icon_task.gif" border="0" align="absmiddle" /> '.$_SWIFT["language"]["task"];
	} else if ($record["statustype"] == STATUS_EVENT) {
		$record["statustype"] = '<img src="'. $_SWIFT["themepath"] .'icon_event.gif" border="0" align="absmiddle" /> '.$_SWIFT["language"]["event"];
	}

	$record["options"] = '<a href="index.php?_m=teamwork&_a=editstatus&calendarstatusid='. $record["calendarstatusid"] .'" title="'. $_SWIFT["language"]["edit"] .'"><img src="'. $_SWIFT["themepath"] .'icon_edit.gif" border="0">&nbsp;'. $_SWIFT["language"]["edit"] .'</a>&nbsp;&nbsp;&nbsp;&nbsp;<a onClick="javascript:doConfirm(\''. $_SWIFT["language"]["actionconfirm"] .'\', \'index.php?_m=teamwork&_a=managestatus&do=delete&calendarstatusid='. $record["calendarstatusid"] .'\');" href="#" title="'. $_SWIFT["language"]["delete"] .'"><img src="'. $_SWIFT["themepath"] .'icon_delete.gif" border="0">&nbsp;'. $_SWIFT["language"]["delete"] .'</a>&nbsp;';

	return $record;
}

/**
* Delete Callback
*/
function _maDeleteCalendarStatus()
{
	global $dbCore, $_SWIFT;

	if ($_SWIFT["isdemo"] == true)
	{
		return false;
	}

	if (is_array($_POST["itemid"]))
	{
		deleteCalendarStatus($_POST["itemid"]);
		$_SWIFT["infomessage"] = $_SWIFT["language"]["statusmuldelconfirm"];
	}
}

/**
* ###############################################
* MANAGE STATUS
* ###############################################
*/
if ($eventaction == "managestatus")
{
	if ($_GET["do"] == "delete" && !empty($_GET["calendarstatusid"]) && $_SWIFT["isdemo"] != true)
	{
		$_SWIFT["infomessage"] = $_SWIFT["language"]["statusmuldelconfirm"];

		deleteCalendarStatus(array($_GET["calendarstatusid"]));
	}

	if (!empty($_GET["insertstatus"]))
	{
		$_SWIFT["infomessage"] = sprintf($_SWIFT["language"]["statusinsertconfirm"], htmlspecialchars($_GET["insertstatus"]));
	} else if (!empty($_GET["updatestatus"])) {
		$_SWIFT["infomessage"] = sprintf($_SWIFT["language"]["statusupdateconfirm"], htmlspecialchars($_GET["updatestatus"]));
	}

	$options["recordsperpage"] = "15";
	$options["sortby"] = "statustype";
	$options["sortorder"] = "asc";
	$options["massaction"][0]["title"] = $_SWIFT["language"]["delete"];
	$options["massaction"][0]["callback"] = "_maDeleteCalendarStatus";

	$options["idname"] = "calendarstatusid";
	$options["quicksearch"] = true;

	$options["advancedsearch"][0]["title"] = $_SWIFT["language"]["title"];
	$options["advancedsearch"][0]["query"] = 'SELECT * FROM `'. TABLE_PREFIX .'calendarstatus` WHERE `title` LIKE \'%$_searchstr%\' $_sortjoin;';

	$options["callback"] = "_calendarStatusFields";

	$options["toolbar"][0]["title"] = $_SWIFT["language"]["insertstatus"];
	$options["toolbar"][0]["url"] = "index.php?_m=teamwork&_a=insertstatus";

	$fields[0]["name"] = "title";
	$fields[0]["title"] = $_SWIFT["language"]["statustitle"];
	$fields[0]["width"] = "";

	$fields[1]["name"] = "statustype";
	$fields[1]["title"] = $_SWIFT["language"]["statustype"];
	$fields[1]["width"] = "180";
	$fields[1]["align"] = "center";

	$fields[2]["name"] = "displayorder";
	$fields[2]["title"] = $_SWIFT["language"]["displayorder"];
	$fields[2]["width"] = "120";
	$fields[2]["align"] = "center";

	$fields[3]["type"] = "custom";
	$fields[3]["name"] = "options";
	$fields[3]["title"] = $_SWIFT["language"]["options"];
	$fields[3]["width"] = "150";
	$fields[3]["align"] = "center";

	$grid = new Grid();

	$interface->adminHeader($_SWIFT["language"]["teamwork"]." > ".$_SWIFT["language"]["managestatus"], 1);

	$interface->adminNavBar('<a href="index.php?_m=core&_a=dashboard" title="'.$_SWIFT["language"]["dashboard"].'">'.$_SWIFT["language"]["dashboard"].'</a> &raquo; <a href="index.php?_m=teamwork&_a=managestatus" title="'.$_SWIFT["language"]["teamwork"].'">'.$_SWIFT["language"]["teamwork"].'</a> &raquo; <a href="index.php?_m=teamwork&_a=managestatus" title="'.$_SWIFT["language"]["managestatus"].'">'.$_SWIFT["language"]["managestatus"].'</a>', $_SWIFT["language"]["desc_managestatus"] , 15);


	$grid->start("teamstatus", $_SWIFT["language"]["statuslist"], 'SELECT * FROM `'. TABLE_PREFIX .'calendarstatus` $_sortjoin;', 'SELECT COUNT(*) AS totalitems FROM `'. TABLE_PREFIX .'calendarstatus`;', 'SELECT * FROM `'. TABLE_PREFIX .'calendarstatus` WHERE `title` LIKE \'%$_searchstr%\' $_sortjoin;', $fields, $options);

	printInfoBox($_SWIFT["infomessage"]);
	printErrorBox($_SWIFT["errormessage"]);

	$grid->display("teamstatus");
	
	$interface->adminFooter();



/**
* ###############################################
* INSERT STATUS
* ###############################################
*/
} else if ($eventaction == "insertstatus") {
	if ($_POST["step"] == "1")
	{
		if (trim($_POST["title"]) == "" || trim($_POST["displayorder"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else if ($_SWIFT["isdemo"] == true) {
			$errormessage = $_SWIFT["language"]["demomode"];
		} else {
			insertCalendarStatus($_POST["statustype"], $_POST["title"], $_POST["statuscolorcode"], $_POST["displayorder"]);

			printRedirect(sprintf($_SWIFT["language"]["statusinsertconfirm"], htmlspecialchars($_POST["title"])), "index.php?_m=teamwork&_a=managestatus&insertstatus=".urlencode($_POST["title"]));
			exit;
		}
	}

	$interface->adminHeader($_SWIFT["language"]["teamwork"]." > ".$_SWIFT["language"]["insertstatus"], 1);

	$interface->adminNavBar('<a href="index.php?_m=core&_a=dashboard" title="'.$_SWIFT["language"]["dashboard"].'">'.$_SWIFT["language"]["dashboard"].'</a> &raquo; <a href="index.php?_m=teamwork&_a=managestatus" title="'.$_SWIFT["language"]["teamwork"].'">'.$_SWIFT["language"]["teamwork"].'</a> &raquo; <a href="index.php?_m=teamwork&_a=managestatus" title="'.$_SWIFT["language"]["managestatus"].'">'.$_SWIFT["language"]["managestatus"].'</a> &raquo; <a href="index.php?_m=teamwork&_a=insertstatus" title="'.$_SWIFT["language"]["insertstatus"].'">'.$_SWIFT["language"]["insertstatus"].'</a>', $_SWIFT["language"]["desc_managestatus"] , 15);

	printInfoBox($_SWIFT["infomessage"]);
	printErrorBox($_SWIFT["errormessage"]);

	renderCalendarStatusForm(INSERT);

	$template->assign("backurl", "index.php?_m=teamwork&_a=managestatus");
	$interface->adminFooter();




/**
* ###############################################
* EDIT STATUS
* ###############################################
*/
} else if ($eventaction == "editstatus") {
	$_calendarstatus = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."calendarstatus` WHERE `calendarstatusid` = '". intval($_REQUEST["calendarstatusid"]) ."';");
	if (empty($_calendarstatus["calendarstatusid"]))
	{
		trigger_error($_SWIFT["language"]["inalidcalstatus"], E_USER_ERROR);
	}

	if ($_POST["step"] == "1")
	{
		if (trim($_POST["title"]) == "" || trim($_POST["displayorder"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else if ($_SWIFT["isdemo"] == true) {
			$errormessage = $_SWIFT["language"]["demomode"];
		} else {
			updateCalendarStatus($_calendarstatus["calendarstatusid"], $_POST["statustype"], $_POST["title"], $_POST["statuscolorcode"], $_POST["displayorder"]);

			printRedirect(sprintf($_SWIFT["language"]["statusupdateconfirm"], htmlspecialchars($_POST["title"])), "index.php?_m=teamwork&_a=managestatus&updatestatus=".urlencode($_POST["title"]));
			exit;
		}
	}

	$_POST = array_merge($_POST, $_calendarstatus);

	$interface->adminHeader($_SWIFT["language"]["teamwork"]." > ".$_SWIFT["language"]["editstatus"], 1);

	$interface->adminNavBar('<a href="index.php?_m=core&_a=dashboard" title="'.$_SWIFT["language"]["dashboard"].'">'.$_SWIFT["language"]["dashboard"].'</a> &raquo; <a href="index.php?_m=teamwork&_a=managestatus" title="'.$_SWIFT["language"]["teamwork"].'">'.$_SWIFT["language"]["teamwork"].'</a> &raquo; <a href="index.php?_m=teamwork&_a=managestatus" title="'.$_SWIFT["language"]["managestatus"].'">'.$_SWIFT["language"]["managestatus"].'</a> &raquo; <a href="index.php?_m=teamwork&_a=editstatus&calendarstatusid='. intval($_calendarstatus["calendarstatusid"]) .'" title="'.$_SWIFT["language"]["editstatus"].'">'.$_SWIFT["language"]["editstatus"].'</a>', $_SWIFT["language"]["desc_managestatus"] , 15);

	printInfoBox($_SWIFT["infomessage"]);
	printErrorBox($_SWIFT["errormessage"]);

	renderCalendarStatusForm(EDIT);

	$template->assign("backurl", "index.php?_m=teamwork&_a=managestatus");
	$interface->adminFooter();
	
}


?>